unit uGxtConfig;

interface

uses
  SysUtils, Classes, ActiveX, uDZSysUtils, uDZStrValidation, uDZPersistent, uDZSerializer,
  uDZConfig;

type
  TGxtConfig = class(TDzPersistent)
  private
    fDBConnection: TDzADOConnectionParam;
    fSmsServiceUrl: string;
    fLBMPSmsIP: string;
    fDebugMode: Boolean;
    fCMCCLBMPUrl2: string;
    fCMCCLBMPUrl1: string;
    fCTCCPrefix: TStrings;
    fCUCCPrefix: TStrings;
    fCMCCPrefix: TStrings;
    procedure SetDBConnection(const Value: TDzADOConnectionParam);
  protected
    procedure ReadProperties(const Reader: IDzClassReader;
      Options: TDzArchiveOptions); override;
    procedure WriteProperties(const Writer: IDzClassWriter;
      Options: TDzArchiveOptions); override;
  public
    constructor Create; override;
    destructor Destroy; override;
    property CMCCPrefix: TStrings read fCMCCPrefix write fCMCCPrefix;
    property CUCCPrefix: TStrings read fCUCCPrefix write fCUCCPrefix;
    property CTCCPrefix: TStrings read fCTCCPrefix write fCTCCPrefix;
  published
    property DBConnection: TDzADOConnectionParam
      read fDBConnection write SetDBConnection;
    property SmsServiceUrl: string read fSmsServiceUrl write fSmsServiceUrl;
    property LBMPSmsIP: string read fLBMPSmsIP write fLBMPSmsIP;
    property CMCCLBMPUrl1: string read fCMCCLBMPUrl1 write fCMCCLBMPUrl1;
    property CMCCLBMPUrl2: string read fCMCCLBMPUrl2 write fCMCCLBMPUrl2;
    property DebugMode: Boolean read fDebugMode write fDebugMode default False;
  end;

var
  GxtConfig: TGxtConfig;

implementation

procedure LoadConfig;
var
  path: string;
  i: Integer;
begin
  path := 'C:\gxtcommon\config.xml';
  ForceDirectories('C:\gxtcommon');
  if FileExists(path) then 
  try
    GxtConfig.LoadFromFile(path, afXML);
  except
  end;
  with GxtConfig, TDzMSAccessADOCP(DBConnection) do
  begin
    if FileName = '' then FileName := 'C:\gxtcommon\lbmp.mdb';
    if SmsServiceUrl = '' then  SmsServiceUrl :=
      'http://59.151.28.107:88/SendSMS.aspx?ececcid=80021191' +
      '&password=123456&msisdn=%s&smscontent=%s&smstype=1';
  end;
  for i := 0 to GxtConfig.CMCCPrefix.Count - 1 do
    CnMobilePhonePrefix.Add(StrToInt(GxtConfig.CMCCPrefix[i]), TObject(0));
  for i := 0 to GxtConfig.CUCCPrefix.Count - 1 do
    CnMobilePhonePrefix.Add(StrToInt(GxtConfig.CUCCPrefix[i]), TObject(1));
  for i := 0 to GxtConfig.CTCCPrefix.Count - 1 do
    CnMobilePhonePrefix.Add(StrToInt(GxtConfig.CTCCPrefix[i]), TObject(2));
end;

function GetGxtConfig: TGxtConfig;
begin
  if not Assigned(GxtConfig) then
  begin
    GxtConfig := TGxtConfig.Create;
    LoadConfig;
  end;
  Result := GxtConfig;
end;

{ TGxtConfig }

constructor TGxtConfig.Create;
begin
  inherited;
  fDBConnection := TDzMSAccessADOCP.Create;
  fCMCCPrefix := TStringList.Create;
  fCUCCPrefix := TStringList.Create;
  fCTCCPrefix := TStringList.Create;
  fCMCCPrefix.Delimiter := ',';
  fCUCCPrefix.Delimiter := ',';
  fCTCCPrefix.Delimiter := ',';
end;

destructor TGxtConfig.Destroy;
begin
  fDBConnection.Free;
  fCMCCPrefix.Free;
  fCUCCPrefix.Free;
  fCTCCPrefix.Free;
  inherited;
end;

procedure TGxtConfig.ReadProperties(const Reader: IDzClassReader;
  Options: TDzArchiveOptions);
var
  str: AnsiString;  
begin
  inherited;
  if Reader.ReadAnsiStr('CMCCPrefix', str) then fCMCCPrefix.DelimitedText := str;
  if Reader.ReadAnsiStr('CUCCPrefix', str) then fCUCCPrefix.DelimitedText := str;
  if Reader.ReadAnsiStr('CTCCPrefix', str) then fCTCCPrefix.DelimitedText := str;
end;

procedure TGxtConfig.SetDBConnection(const Value: TDzADOConnectionParam);
begin
  fDBConnection.Assign(Value);
end;

procedure TGxtConfig.WriteProperties(const Writer: IDzClassWriter;
  Options: TDzArchiveOptions);
begin
  inherited;
  Writer.WriteAnsiStr('CMCCPrefix', fCMCCPrefix.DelimitedText);
  Writer.WriteAnsiStr('CUCCPrefix', fCUCCPrefix.DelimitedText);
  Writer.WriteAnsiStr('CTCCPrefix', fCTCCPrefix.DelimitedText);
end;

initialization
  CoInitialize(nil);
  GetGxtConfig;
  
finalization
  CheckFreeAndNil(GxtConfig);
  CoUnInitialize;

end.
